<?php 
/**
 * 数据库数据检查返回对应信息
 **/
namespace App\Tools;

use Illuminate\Support\Facades\Auth;
use App\Models\Order;
use App\Models\Shop;
use App\Models\UserCoupon;
class SqlCheck{
    //检查是否优惠 及 房桌
    public function checkShop($shop_id,$type){
        $shop = Shop::where('is_check',1)->with('shopinfo')->with('shopcheapen')->find($shop_id);
        if(!$shop){
            return show(201,'店铺不存在');
        }
        switch ($type){
            case 'takeout'://外卖
                //获取我的优惠券
                if(Auth::id()){
                    $usercoupon=UserCoupon::where('user_id',Auth::id())
                        ->whereIn('shop_id',[-1,$shop_id])
                        ->where('is_use',0)
                        ->with('coupondetail')
                        ->get();
                }
                $shop->usercoupon = $usercoupon;
                return show(200,'已检',$shop);
                break;
            case 'book'   ://预定
                return show(200,'已检',$shop);
                break;
            case 'sweep'  ://扫码
                return show(200,'已检',$shop);
                break;
            default :
                return show(201,'下单类型不存在');
                break;
        }
    }
    //检查支付订单
    public function checkOrder($order_id)
    {
        try {
            $order = Order::find($order_id);
            if (!$order) {
                return show(201, '订单不存在');
            }
            switch ($order->order_type) {
                case 0://外卖
                    if ($order->order_status != 1) {
                        return show(202, '该订单不可支付');
                    }
                    break;

                case 1://预定
                    if ($order->order_status != 1 && $order->order_status != 3) {
                        return show(202, '该订单不可支付');
                    }
                    break;
                case 2://排队
                    if ($order->order_status != 1) {
                        return show(202, '该订单不可支付');
                    }
                    break;

                case 3://扫码
                    if ($order->order_status != 1) {
                        return show(202, '该订单不可支付');
                    }
                    break;
                case 4://直接付款
                case 5://充值
                case 6://优惠卡
                    if ($order->order_status != 1) {
                        return show(202, '该订单不可支付');
                    }
                    break;
                default:
                    return show(202, '该订单不可支付');
                    break;
            }
            return show(200, '已检查', $order);
        }catch (\Exception $e){
            return show(203, '未知错误');
        }
    }

}

